package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattleCampScoreRecord;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangBattleDataDaoImpl extends TempDataObjectDao<CrossKuangBattleData> {

    public static String getCrossKuangBattleDataSql = "select * from `t_c_kuang_battle_data` where `weekId`=? and `groupId`=?";

    public CrossKuangBattleData getCrossKuangBattleData(int weekId, int groupId) {
        CrossKuangBattleData crossKuangBattleData = null;
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangBattleDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                crossKuangBattleData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return crossKuangBattleData;
    }

    public static String getCrossKuangBattleGroupListSql = "select groupId from `t_c_kuang_battle_data` where `weekId`=? ";

    public List<Integer> getCrossKuangBattleGroupList(int weekId) {
        List<Integer> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangBattleGroupListSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(rs.getInt("groupId"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertCrossKuangBattleDataSql = "insert into `t_c_kuang_battle_data`(weekId,groupId,round,battleEndHandle,rankReward,campScoreRecordMap,updateTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossKuangBattleData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBattleEndHandle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isRankReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampScoreRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertCrossKuangBattleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossKuangBattleDataSql = "update `t_c_kuang_battle_data` set round=?,battleEndHandle=?,rankReward=?,campScoreRecordMap=?,updateTime=? where weekId=? and groupId =?";
    @Override
    protected boolean updateData(CrossKuangBattleData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBattleEndHandle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isRankReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampScoreRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossKuangBattleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossKuangBattleData parseData(ResultSet rs) throws SQLException {
        CrossKuangBattleData data = new CrossKuangBattleData();
        data.setWeekId(rs.getInt("weekId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setRound(rs.getInt("round"));
        data.setBattleEndHandle(rs.getBoolean("battleEndHandle"));
        data.setRankReward(rs.getBoolean("rankReward"));
        data.setCampScoreRecordMap(JSON.parseObject(rs.getString("campScoreRecordMap"), new TypeReference<Map<Integer, Map<Integer, CrossKuangBattleCampScoreRecord>>>() {
        }));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }
}
